//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		account:app.setting.account,
		ShowAuthM:false,
		pagetitle:'升级会员',
		
	},
	onReady: function (){
		
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		app.util.get(app.access,function(pagedata){
			page.setData({
				pagedata:pagedata,
				pluginnav:app.session.menus,
				ShowAuthM:pagedata.show_setmember==2?true:false,
				action:app.access.plugin+'/'+app.access.action+'/'
			});
		});
	},
	onChooseAvatar:function(e){
		//获取头像
		var the=this;
		var avatarUrl= e.detail.avatarUrl
		the.setData({
			avatarUrl:avatarUrl
		})
		app.avatarUrl=avatarUrl
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		var the=this;
		app.util.getUserInfo(e,function(res){
			if(res && res.userInfo){
				app.isgetUserInfo=true
				the.setData({
					ShowAuthM:false
				})
				the.onLoad();
			}else{
				return false;
			}
		});
	},
	mobile_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户手机号
		var the = this
		var pagedata= the.data.pagedata
		app.util.post('core/member.login/wxapp_login/return_mobile=1',e.detail,function(res){
			the.setData({
				ShowAuthM:false
			})
		});
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
	setshowsheng_fees:function(){
		var the=this;
		var showsheng_fees=the.data.showsheng_fees?false:true;
		the.setData({
			showsheng_fees:showsheng_fees
		})
	},
	setshowgoods:function(){
		var the=this;
		var showgoods=the.data.showgoods?false:true;
		the.setData({
			showgoods:showgoods
		})
	},
	setshowcoupon:function(){
		var the=this;
		var showcoupon=the.data.showcoupon?false:true;
		the.setData({
			showcoupon:showcoupon
		})
	},
	setshow_xieyi:function(){
		var the=this;
		var show_xieyi=the.data.show_xieyi?false:true;
		the.setData({
			show_xieyi:show_xieyi
		})
	},
	setcheck:function(){
		var the=this;
		var pagedata=the.data.pagedata;
		var checked=pagedata.post.checked?false:true;
		the.setData({
			'pagedata.post.checked':checked,
			show_xieyi:false
		})
	},
	setnowlevel:function(e){
		var the=this;
		var pagedata=the.data.pagedata;
		var data=e.currentTarget.dataset;
		var item=data.item;
		the.setData({
			'pagedata.post.level_id':item.id,
			'pagedata.nowlevel':item,
		})
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	click_tab:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util.tab(data.params);
	},
	closepaymodal:function(){
		var page=this
		page.setData({
			showpaymodal:false
		});
	},
	pay_credit:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_credit(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open)
	},
	pay_wechat:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_wechat(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open);
	},
	pay_alipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_alipay(pagedata.paymodal.id,function(res){
				page.setData({
						showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open);
	},
    pay_ttpay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_ttpay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open)
	},
	pay_kspay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_kspay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open)
	},
	pay_hdfk:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_hdfk(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		})
	},
	pay_apipay:function(e){
		var page=this
		var pagedata=page.data.pagedata
		app.util.pay_apipay(pagedata.paymodal.id,function(res){
				page.setData({
					showpaymodal:false,
				});
				app.util.msg(res.message,'',pagedata.homeaction);
		},pagedata.paymodal.di_open,pagedata.paymodal.api_name)
	},
	radiotype:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		the.setData({
			'pagedata.paymodal.paytype':data.type,
			'pagedata.paymodal.api_name':data.name
		})
	},
	radiodi_open:function(e){
		var the=this;
		var value=e.detail.value;
		var data=e.currentTarget.dataset;
		var pagedata=the.data.pagedata
		var di_open=pagedata.paymodal.di_open;
		di_open=di_open==data.type?'':data.type;
		if(!di_open){
			data.difee=0;
		}
		the.setData({
			'pagedata.paymodal.di_open':di_open,
			'pagedata.paymodal.di_fee':data.difee
		})
	},
	new_gotopay:function(e){
		var the=this;
		var pagedata=the.data.pagedata
		var paytype=pagedata.paymodal.paytype;
		if(paytype=='credit'){
			the.pay_credit();
		}else if(paytype=='wechat'){
			the.pay_wechat();
		}else if(paytype=='alipay'){
			the.pay_alipay();
		}else if(paytype=='hdfk'){
			the.pay_hdfk();
		}else if(paytype=='apipay'){
			the.pay_apipay();
		}else if(paytype=='ttpay'){
			the.pay_ttpay();
		}else if(paytype=='kspay'){
			the.pay_kspay();
		}else{
			app.util.msg('请选择支付方式！');
		}
	},
	buyvip:function(event){
		var page=this
		var pagedata=page.data.pagedata
		if(!pagedata.post.checked){
			app.util.msg('请先阅读并勾选《会员协议》','error');
			return false;
		}
		if(pagedata.nowid[pagedata.post.level_id]>0){
			app.util.post('jy_weishop/member.open/pay/id='+pagedata.nowid[pagedata.post.level_id],pagedata.post,function(pay_id){
				console.log('pay_id');console.log(pay_id);
				app.util.pay(pay_id,function(res){	
					if(res.type=='more'){
						page.setData({
							showpaymodal:true,
							'pagedata.paymodal':res
						});
					}else if(res.type=='success'){
						app.util.msg(res.message,'',pagedata.homeaction);
					}else{
						app.util.msg(res.message);
					}
				});
			});
		}else{
			app.util.post('jy_weishop/member.open/post/',pagedata.post,function(res){
				console.log('pay_id');console.log(res);
				pagedata.nowid[pagedata.post.level_id]=res.order_id
				page.setData({
					'pagedata.nowid':pagedata.nowid
				});
				app.util.pay(res.pay_id,function(res){
					//console.log('收到支付结果！');console.log(res);
					if(res.type=='more'){
						page.setData({
							showpaymodal:true,
							'pagedata.paymodal':res
						});
					}else if(res.type=='success'){
						app.util.msg(res.message,'',pagedata.homeaction);
					}else{
						app.util.msg(res.message);
					}
				},'','',pagedata.homeaction);
			});
		}
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('jy_weishop/member.index/post/',data);
	}
});